package com.qnnproject

import android.content.Context
import android.content.SharedPreferences
import android.provider.Settings
import java.util.UUID

class DeviceIdManager(private val context: Context) {
    
    private val prefs: SharedPreferences = context.getSharedPreferences("device_prefs", Context.MODE_PRIVATE)
    private val DEVICE_ID_KEY = "device_id"
    
    /**
     * 获取设备ID，如果不存在则生成一个新的
     */
    fun getDeviceId(): String {
        var deviceId = prefs.getString(DEVICE_ID_KEY, null)
        
        if (deviceId == null) {
            // 尝试获取Android ID
            val androidId = Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
            
            if (androidId != null && androidId != "9774d56d682e549c") {
                // 使用Android ID作为设备ID
                deviceId = "android_${androidId}"
            } else {
                // 生成一个UUID作为设备ID
                deviceId = "device_${UUID.randomUUID().toString().replace("-", "").substring(0, 16)}"
            }
            
            // 保存设备ID
            prefs.edit().putString(DEVICE_ID_KEY, deviceId).apply()
        }
        
        return deviceId
    }
    
    /**
     * 设置自定义设备ID
     */
    fun setDeviceId(deviceId: String) {
        prefs.edit().putString(DEVICE_ID_KEY, deviceId).apply()
    }
    
    /**
     * 获取队列名称
     */
    fun getQueueName(): String {
        return "call_task_${getDeviceId()}"
    }
}
